package com.moneywelcome.orderassembly.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.orderassembly.model.InvestRecord;
import com.moneywelcome.orderassembly.model.ProfitDay;
import com.moneywelcome.orderassembly.mapper.ProfitDayMapper;
import com.moneywelcome.orderassembly.service.ProfitDayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moneywelcome.orderfacade.dto.ProfitDayDto;
import com.moneywelcome.orderfacade.dto.SubscribeOrderDto;
import com.moneywelcome.orderfacade.vo.OrderPageVo;
import com.moneywelcome.orderfacade.vo.ProfitDayVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.StatusCode;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户每日盈亏表 服务实现类
 * </p>
 *
 * @author alice
 * @since 2022-01-06
 */
@Service
public class ProfitDayServiceImpl extends ServiceImpl<ProfitDayMapper, ProfitDay> implements ProfitDayService {

    @Override
    public PageResult<ProfitDayDto> queryRecord(OrderPageVo op) {
        QueryWrapper<ProfitDay> qw = new QueryWrapper<>();
        if (null != op.getUserId())  qw.eq("user_id", op.getUserId());
        if (null!=op.getFinancialId()) qw.eq("financial_id",op.getFinancialId());
        if (null != op.getStartTime()) qw.ge("profit_time", op.getStartTime());
        if (null != op.getEndTime()) qw.le("profit_time", op.getEndTime());
        Page<ProfitDay> page = page(new Page<>(op.getCurrent(), op.getSize()), qw);
        PageResult pageResult = BeanUtil.copyProperties(page, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(page.getRecords(), ProfitDayDto.class));
        pageResult.setTotalCount(page.getTotal());
        return pageResult;
    }

    @Override
    public ProfitDayDto create(ProfitDayVo profitDayVo) {
        ProfitDay profit = BeanUtil.copyProperties(profitDayVo, ProfitDay.class);
        profit.setProfitTime(DateUtil.now());
        if(!save(profit)) throw new MwcArgsException("收益保存失败", StatusCode.Error);
        return BeanUtil.copyProperties(profit,ProfitDayDto.class);
    }
}
